# -*- coding: utf-8 -*-
from flask.ext.mail import Message
from flask import render_template
from app import mail, app
from config import ADMINS
from decorators import async

__author__ = 'nibo'

@async
def send_async_email(msg):
    with app.app_context():
        mail.send(msg)


def send_email(subject, sender, recipients, txt_body, html_body):
    msg = Message(subject, sender=sender, recipients=recipients)
    msg.body = txt_body
    msg.html = html_body
    send_async_email(msg)


def follower_notification(followed, follower):
    send_email(u"[microblog] %s 正在关注您!" % follower.nickname,
               ADMINS[0],
               ['nibome@163.com'],
               render_template("follower_email.txt",
                               user=followed, follower=follower),
               render_template("follower_email.html",
                               user=followed, follower=follower))
